home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / ERRFIX.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  2KB  |  52 lines

  1. /*
  2. ** ERRFIX.C - redirect stderr to some other file under MS-DOS
  3. **
  4. ** by Bob Jarvis
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <process.h>
  10.  
  11. char *usage = "ERRFIX [filename] [prog] { {parm1} {parm2} ... {parmN} }\n"
  12.               "   Redirects stderr to another file, then invokes a program\n"
  13.               "   which will inherit the new definition of stderr.\n\n"
  14.               "Parameters:\n"
  15.               "   filename (required) - the name of the file stderr should\n"
  16.               "      be redirected to.  Output written to stderr will\n"
  17.               "      be routed to this file instead of the console.\n"
  18.               "   prog (required) - name of the program to be run.\n"
  19.               "   parm1...parmN (optional) - command-line parameters needed\n"
  20.               "      to run the program specified by the 'prog' argument.";
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24.       char **args = argv;
  25.  
  26.       if (3 > argc)
  27.       {
  28.             printf(usage);
  29.             return 1;
  30.       }
  31.  
  32.       if (NULL != argv[argc]) /* may be a problem under some compilers */
  33.       {
  34.             args = malloc((argc+1) * sizeof(char *));
  35.             if (NULL == args)
  36.             {
  37.                   printf("Unable to allocate storage");
  38.                   return 2;
  39.             }
  40.  
  41.             memcpy(args, argv, argc * sizeof(char *));
  42.  
  43.             args[argc] = NULL;
  44.       }
  45.  
  46.       freopen(args[1], "w", stderr);
  47.  
  48.       spawnvp(0, args[2], &args[2]);
  49.  
  50.       return 0;
  51. }
  52.